package com.example.fill;

import static java.lang.Thread.sleep;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import androidx.annotation.NonNull;

import com.example.fill.Test240126.Commend;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class View240126 extends SurfaceView implements SurfaceHolder.Callback {

    Paint linePaint = new Paint();
    Paint tp = new Paint();
    Paint dotPaint = new Paint();
    Paint red = new Paint();
    Paint yellow = new Paint();

    private final List<Commend> bean = new ArrayList<>();
    Rect rect = new Rect();

    public View240126(Context context) {
        super(context);
        getHolder().addCallback(this);
        tp.setColor(Color.BLACK);
        tp.setStrokeWidth(5f);
        tp.setTextSize(28);

        linePaint.setColor(Color.BLACK);
        linePaint.setStyle(Paint.Style.STROKE);
        linePaint.setStrokeWidth(5f);
        linePaint.setTextSize(48);

        dotPaint.setColor(Color.GREEN);
        dotPaint.setStyle(Paint.Style.FILL);
        dotPaint.setStrokeWidth(8f);

        red.setColor(Color.RED);
        red.setStyle(Paint.Style.STROKE);
        red.setStrokeWidth(8f);

        yellow.setColor(Color.YELLOW);
        yellow.setStyle(Paint.Style.STROKE);
        yellow.setStrokeWidth(8f);
    }

    public View240126(Context context, AttributeSet attrs) {
        super(context, attrs);
        getHolder().addCallback(this);
        tp.setColor(Color.BLACK);
        tp.setStrokeWidth(5f);
        tp.setTextSize(28);

        linePaint.setColor(Color.BLACK);
        linePaint.setStyle(Paint.Style.STROKE);
        linePaint.setStrokeWidth(5f);
        linePaint.setTextSize(48);
        dotPaint.setColor(Color.GREEN);
        dotPaint.setStyle(Paint.Style.FILL);
        dotPaint.setStrokeWidth(8f);

        red.setColor(Color.RED);
        red.setStyle(Paint.Style.STROKE);
        red.setStrokeWidth(8f);

        yellow.setColor(Color.YELLOW);
        yellow.setStyle(Paint.Style.STROKE);
        yellow.setStrokeWidth(8f);
    }

    public View240126(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        tp.setColor(Color.BLACK);
        tp.setStrokeWidth(5f);
        tp.setTextSize(28);

        getHolder().addCallback(this);
        linePaint.setColor(Color.BLACK);
        linePaint.setStyle(Paint.Style.STROKE);
        linePaint.setStrokeWidth(5f);
        linePaint.setTextSize(48);
        dotPaint.setColor(Color.GREEN);
        dotPaint.setStyle(Paint.Style.FILL);
        dotPaint.setStrokeWidth(8f);

        red.setColor(Color.RED);
        red.setStyle(Paint.Style.STROKE);
        red.setStrokeWidth(8f);

        yellow.setColor(Color.YELLOW);
        yellow.setStyle(Paint.Style.STROKE);
        yellow.setStrokeWidth(8f);
    }

    public View240126(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

//    public void setBean(List<Commend> bean) {
//        this.bean.clear();
//        this.bean.addAll(bean);
//        if (bean.isEmpty()) return;
//        first = bean.get(0).getPushTime();
//        end = getWidth() + first;
//        invalidate();
//    }

    public void addCommend(Commend c) {
        this.bean.add(c);
        end = c.getPushTime() / 10L;
        first = end - getWidth();
    }

    int p = 20;
    Path path = new Path();
    SimpleDateFormat sdf = new SimpleDateFormat("ss", Locale.CANADA);

    ExecutorService executorService = Executors.newSingleThreadExecutor();

    @Override
    public void surfaceCreated(@NonNull SurfaceHolder holder) {
        this.holder = holder;
        MyThread myThread = new MyThread();
        new Thread(myThread).start();
    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
        this.holder = null;
    }

    SurfaceHolder holder = null;
    boolean run = true;

    class MyThread implements Runnable {
        @Override
        public void run() {
            while (run) {
                Canvas canvas = holder.lockCanvas();
                if (canvas == null) continue;
                canvas.drawColor(Color.WHITE);

                int width = getWidth();
                int height = getHeight();
                int hd2 = height / 2;
                int firstmh = (int) (first - 200);

                String sss = first + "/" + end;
                linePaint.getTextBounds(sss, 0, sss.length(), rect);
                canvas.drawText(sss, 0, rect.height(), linePaint);
                canvas.drawLine(0, hd2, width, hd2, linePaint);

                int j = 1;
                for (int i = 0; i < bean.size(); i++) {
                    Commend commend = bean.get(i);
                    long s = (commend.getPushTime() / 10L - first);
                    canvas.drawPoint(s, hd2, dotPaint);
                    String push = sdf.format(commend.getPushTime());
                    canvas.drawText(push, s, hd2, tp);

                    if (commend.getPushTime()/10L <  firstmh) continue;

                    if (commend.isNoBack()) {
                        long b = commend.getBackTime() / 10L - first;
                        path.moveTo(s, hd2 + p * j);
                        path.lineTo(b, hd2 + p * j);
                        canvas.drawPath(path, red);
                    } else if (!commend.isNoBack() && commend.getBackTime() == 0) {
                        canvas.drawPoint(getWidth(), hd2, dotPaint);
                        path.moveTo(s, hd2);
                        path.lineTo(s, hd2 + p * j);
                        path.lineTo(width, hd2 + p * j);
                        path.lineTo(width, hd2);
                        canvas.drawPath(path, yellow);
                    } else {
                        long b = commend.getBackTime() / 10L - first;
                        canvas.drawPoint(b, hd2, dotPaint);
                        String back = sdf.format(commend.getBackTime());
                        canvas.drawText(back, b, hd2, tp);
                        path.moveTo(s, hd2);
                        path.lineTo(s, hd2 + p * j);
                        path.lineTo(b, hd2 + p * j);
                        path.lineTo(b,hd2);
                        canvas.drawPath(path, linePaint);
                    }
                    j++;
                    path.reset();
                }
                j = 1;
                holder.unlockCanvasAndPost(canvas);
            }
        }
    }

    float downX = 0;
    float downY = 0;
    long first = 0;

    long end = 0;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = event.getX();
                downY = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                int moveX = (int) event.getX();
                int deltaX = (int) (moveX - downX);
                int newScrollX = getScrollX() - deltaX;
                first += newScrollX;
                end += newScrollX;
                downX = moveX;
                break;
            case MotionEvent.ACTION_UP:
                performClick();
                break;
        }
        return true;
    }

    @Override
    public boolean performClick() {
        return super.performClick();
    }
}
